﻿#include "SelectDataBase.h"
#include "ui_SelectDataBase.h"

SelectDataBase::SelectDataBase(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SelectDataBase)
{
    ui->setupUi(this);
    setWindowTitle(u8"选择一个数据库");
    m_sqlDirPath = ui->lineEdit->text();
//    initTableWidget();
}

SelectDataBase::~SelectDataBase()
{
    delete ui;
}

void SelectDataBase::initTableWidget()
{
    //清除表格
    int rowCount = ui->tableWidget->rowCount();
    for(int r = rowCount-1 ; r >=0 ; r--)
    {
        ui->tableWidget->removeRow(r);
    }
    //初始化表格
    ui->tableWidget->setColumnCount(3);     //设置三列
    ui->tableWidget->setHorizontalHeaderLabels(QStringList() << u8"Panel名称"
                                               << u8"详细信息"
                                               << u8"测点数量");
    ui->tableWidget->setAlternatingRowColors(true);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);           //设置整行选中
    //ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);//设置列距固定
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);               //设置最后一行自动填满
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);            //设置不能编辑

    ui->tableWidget->setColumnWidth(0, 150);
    ui->tableWidget->setColumnWidth(1, 450);

    QStringList filePath = findFile(m_sqlDirPath);      //传入目录，寻找目录内数据库文件
    for(int i = 0; i < filePath.count(); i++)
    {
        //读取数据库
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName( filePath[i] );
        if (db.open())
        {
            QSqlQuery query;
            query.exec("SELECT * FROM panelinfo");
            query.next();
            QString name = query.value(0).toString();
            QString title = query.value(1).toString();
            QString paris = query.value(2).toString();

            int row = ui->tableWidget->rowCount();      //获取当前数据行数
            ui->tableWidget->insertRow(row);            //在末尾插入一行
            ui->tableWidget->setItem(row,0,new  QTableWidgetItem(name));
            ui->tableWidget->setItem(row,1,new  QTableWidgetItem(title));
            ui->tableWidget->setItem(row,2,new  QTableWidgetItem(paris));
        }
        db.close();
    }
}

QStringList SelectDataBase::findFile(const QString &strFilePath)
{
    QStringList filePath;
    QDir dir(strFilePath);
    QDirIterator iter(dir.absolutePath(),               //参数： 目录 + 条件1 + 条件N
                      QStringList() << "*.db",          //条件1 后缀名为 .db
                      QDir::NoSymLinks | QDir::Files,   //条件2 不列出符号链接 只列出文件
                      QDirIterator::FollowSymlinks);    //条件3 列出子目录中的条目
    while (iter.hasNext())  //如果有下条
    {
        iter.next();        //转到下条记录
        QFileInfo info=iter.fileInfo();
        if (info.isFile())
        {
            filePath << info.filePath();
        }
    }
    return filePath;
}


void SelectDataBase::on_pushButton_Open_clicked()
{
    QString Name;
    int row = ui->tableWidget->currentRow();
    if(row != -1)
    {
        Name =  ui->tableWidget->item(row , 0)->text();
    }
    if(Name.isEmpty())
    {
        QMessageBox::information(this , u8"提示" , u8"请选中一条信息！");
        return;
    }
    emit selectOK(ui->tableWidget->item(ui->tableWidget->currentRow(), 0)->text(), m_sqlDirPath);
    this->close();
}

void SelectDataBase::on_pushButton_SelectDir_clicked()
{
    QString fileName = QFileDialog::getExistingDirectory(NULL,
                                                         u8"选择文件夹",
                                                         "D:/SQLite");
    if(fileName.isNull())
    {
        return;
    }
    ui->lineEdit->setText(fileName);
    m_sqlDirPath = fileName;

    initTableWidget();
}


void SelectDataBase::on_tableWidget_doubleClicked(const QModelIndex &index)
{
    QString Name;
    int row = ui->tableWidget->currentRow();
    if(row != -1)
    {
        Name =  ui->tableWidget->item(row , 0)->text();
    }
    if(Name.isEmpty())
    {
        QMessageBox::information(this , u8"提示" , u8"请选中一条信息！");
        return;
    }
    emit selectOK(ui->tableWidget->item(ui->tableWidget->currentRow(), 0)->text(), m_sqlDirPath);
    this->close();
}

void SelectDataBase::on_pushButton_clicked()
{
    QFileDialog::getOpenFileName(this, u8"文件管理", "D:/SQLite", u8"数据库 (*.db)");
}
